package com.body.park.user.business;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.body.park.user.client.UserService;
import com.body.park.user.client.model.UserResult;
import com.body.park.user.entity.TUser;
import com.body.park.user.service.ITUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author chaoxian.wu
 * @DATE 2022/4/5 10:18
 */
@Slf4j
@DubboService
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private ITUserService userService;

    @Override
    public UserResult findUserByUserName(String userName) {
        TUser user = userService.getOne(new QueryWrapper<TUser>().lambda().eq(TUser::getName, userName));
        log.info("用户信息是：{}", JSONUtil.toJsonStr(user));
        return buildResult(user);
    }

    /**
     * 构建返回对象
     * @param user
     * @return
     */
    private UserResult buildResult(TUser user) {
        if (user != null) {
            UserResult result = new UserResult();
            BeanUtils.copyProperties(user, result);
            return result;
        }
        return null;
    }
}
